
package com.shide.businessuser.mvvm;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 网络请求类型实体，为了方便与view互动处理UI
 * T为实体类
 * @author gehj
 * @date 2021/1/20 16:12
 */
public class Resource<T> {
    @NonNull
    public final Status status;//网络请求状态
    @Nullable
    public final T data;//数据实体
    @Nullable public final String message;

    public enum Status { SUCCESS, ERROR, LOADING }

    private Resource(@NonNull Status status, @Nullable T data,
                     @Nullable String message) {
        this.status = status;
        this.data = data;
        this.message = message;
    }


    /**
     * 正在请求网络数据
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Resource<T> loading(@Nullable T data) {
        return new Resource<>(Status.LOADING, data, null);
    }


    /**
     * 请求成功
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Resource<T> success(@NonNull T data) {
        return new Resource<>(Status.SUCCESS, data, null);
    }

    /**
     * 请求出错
     * @param msg
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Resource<T> error(String msg, @Nullable T data) {
        return new Resource<>(Status.ERROR, data, msg);
    }

}
